package com.ruoyi.central.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.central.domain.TopCentralOperationStation;
import com.ruoyi.central.service.ITopCentralOperationStationService;
import com.ruoyi.central.service.ITopCentralProductTypeService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.service.ServiceUtils;
import com.ruoyi.common.validation.group.InsertGroup;
import com.ruoyi.common.validation.group.UpdateGroup;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.utils.bean.SelectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.central.domain.TopCentralProductTypeOperationStation;
import com.ruoyi.central.service.ITopCentralProductTypeOperationStationService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.validation.group.UpdateBatchGroup;
import com.ruoyi.common.core.domain.StateVo;
import java.util.Collection;
import java.util.Map;

/**
 * 产品类型与操作工位Controller
 * 
 * @author ruoyi
 * @date 2022-03-02
 */
@Controller
@RequestMapping("/central/centralProductTypeOperationStation")
public class TopCentralProductTypeOperationStationController extends BaseController
{
    private String prefix = "central/centralProductTypeOperationStation";

    @Autowired
    private ITopCentralProductTypeOperationStationService topCentralProductTypeOperationStationService;

    @Autowired
    private ITopCentralProductTypeService productTypeService;

    @Autowired
    private ITopCentralOperationStationService operationStationService;

    @RequiresPermissions("central:centralProductTypeOperationStation:view")
    @GetMapping()
    public String centralProductTypeOperationStation()
    {
        return prefix + "/centralProductTypeOperationStation";
    }

    /**
     * 根据产品类型获取 产品类型与操作工位 详细列表
     * @param productTypeId
     * @return
     */
    @GetMapping("/listByProductTypeId/{productTypeId}")
    @ResponseBody
    public AjaxResult listByProductTypeId(@PathVariable("productTypeId") Integer productTypeId){

        Collection<TopCentralProductTypeOperationStation> productTypeOperationStationList = topCentralProductTypeOperationStationService.selectAllMap().values();

        //根据产品类型编号分组的容器
        List<TopCentralProductTypeOperationStation> productTypeOperationStationListOfProductTypeIdGroup = new ArrayList<>();

        productTypeOperationStationList.forEach(productTypeOperationStation -> {
            if(StringUtils.equals(productTypeId,productTypeOperationStation.getProductTypeId())){
                productTypeOperationStationListOfProductTypeIdGroup.add(productTypeOperationStation);
            }
        });
        List<TopCentralOperationStation> operationStations = new ArrayList<>();
        if(StringUtils.isNotEmpty(productTypeOperationStationListOfProductTypeIdGroup)){
            Map<? extends Serializable, TopCentralOperationStation> operationStationMap = operationStationService.selectAllMap();
            for (TopCentralProductTypeOperationStation topCentralProductTypeOperationStation : productTypeOperationStationListOfProductTypeIdGroup) {
                TopCentralOperationStation operationStation = operationStationMap.get(topCentralProductTypeOperationStation.getOperationStationId());
                if(StringUtils.isNotNull(operationStation)){
                    operationStations.add(operationStation);
                }
            }
        }
        return AjaxResult.success(SelectUtils.buildSelectList(operationStations,TopCentralOperationStation::getId,TopCentralOperationStation::getName));
    }

    /**
     * 查询产品类型与操作工位列表
     */
    @RequiresPermissions("central:centralProductTypeOperationStation:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(TopCentralProductTypeOperationStation topCentralProductTypeOperationStation)
    {
        startPage();
        List<TopCentralProductTypeOperationStation> list = topCentralProductTypeOperationStationService.selectList(topCentralProductTypeOperationStation);

        ServiceUtils.complete(list,productTypeService,((productTypeMap,item)->item.setProductType(productTypeMap.get(item.getProductTypeId()))));
        ServiceUtils.complete(list,operationStationService,((operationStationMap,item)->item.setOperationStation(operationStationMap.get(item.getOperationStationId()))));

        return getDataTable(list);
    }

    /**
     * 查询简单的产品类型与操作工位列表 用于select选择器
     */
    @GetMapping("/simpleList")
    @ResponseBody
    public AjaxResult simpleList(TopCentralProductTypeOperationStation topCentralProductTypeOperationStation){

        Collection<TopCentralProductTypeOperationStation> topCentralProductTypeOperationStationList = topCentralProductTypeOperationStationService.selectAllMap().values();

        return AjaxResult.success(SelectUtils.buildSelectList(topCentralProductTypeOperationStationList,TopCentralProductTypeOperationStation::getId,TopCentralProductTypeOperationStation::getOperationStationId));
    }

    /**
     * 获取产品类型与操作工位详细信息
     */
    @RequiresPermissions("central:centralProductTypeOperationStation:query")
    @GetMapping(value = "/{id}")
    @ResponseBody
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(topCentralProductTypeOperationStationService.selectById(id));
    }

    /**
     * 获取自增排序值
     * @param topCentralProductTypeOperationStation 过滤条件
     * @return
     */
    @GetMapping("/getIncrementWeight")
    @ResponseBody
    public AjaxResult getIncrementWeight(TopCentralProductTypeOperationStation topCentralProductTypeOperationStation){
        int weigth = topCentralProductTypeOperationStationService.getMaxOfWeight(topCentralProductTypeOperationStation);
        return AjaxResult.success(++weigth);
    }

    /**
     * 下载产品类型与操作工位导入模板
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<TopCentralProductTypeOperationStation> util = new ExcelUtil<>(TopCentralProductTypeOperationStation.class);
        util.importTemplateExcel(response, "产品类型与操作工位数据");
    }

    /**
     * 导入产品类型与操作工位数据
     * @param file 产品类型与操作工位数据文件
     * @param updateSupport 是否为更新
     */
    @Log(title = "产品类型与操作工位", businessType = BusinessType.IMPORT)
    @RequiresPermissions("central:centralProductTypeOperationStation:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TopCentralProductTypeOperationStation> util = new ExcelUtil<>(TopCentralProductTypeOperationStation.class);
        List<TopCentralProductTypeOperationStation> topCentralProductTypeOperationStationList = util.importExcel(file.getInputStream());

        if(updateSupport){
            if(topCentralProductTypeOperationStationService.updateBatchById(topCentralProductTypeOperationStationList)){
                return AjaxResult.success("导入产品类型与操作工位数据覆盖成功！");
            }
            return AjaxResult.error("导入产品类型与操作工位数据覆盖失败！");
        }
        if(topCentralProductTypeOperationStationService.insertBatch(topCentralProductTypeOperationStationList)){
            return AjaxResult.success("导入产品类型与操作工位数据新增成功！");
        }
        return AjaxResult.error("导入产品类型与操作工位数据新增失败！");
    }

    /**
     * 导出产品类型与操作工位列表
     */
    @RequiresPermissions("central:centralProductTypeOperationStation:export")
    @Log(title = "产品类型与操作工位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TopCentralProductTypeOperationStation topCentralProductTypeOperationStation)
    {
        List<TopCentralProductTypeOperationStation> list = topCentralProductTypeOperationStationService.selectList(topCentralProductTypeOperationStation);
        ExcelUtil<TopCentralProductTypeOperationStation> util = new ExcelUtil<TopCentralProductTypeOperationStation>(TopCentralProductTypeOperationStation.class);
        return util.exportExcel(list, "产品类型与操作工位数据");
    }

    /**
     * 新增产品类型与操作工位
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存产品类型与操作工位
     */
    @RequiresPermissions("central:centralProductTypeOperationStation:add")
    @Log(title = "产品类型与操作工位", businessType = BusinessType.INSERT)
    @PostMapping
    @ResponseBody
    public AjaxResult addSave(@Validated(InsertGroup.class) @RequestBody TopCentralProductTypeOperationStation topCentralProductTypeOperationStation)
    {
        return toAjax(topCentralProductTypeOperationStationService.insert(topCentralProductTypeOperationStation));
    }

    /**
     * 修改产品类型与操作工位
     */
    @RequiresPermissions("central:centralProductTypeOperationStation:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        TopCentralProductTypeOperationStation topCentralProductTypeOperationStation = topCentralProductTypeOperationStationService.selectById(id);
        mmap.put("topCentralProductTypeOperationStation", topCentralProductTypeOperationStation);
        return prefix + "/edit";
    }

    /**
     * 修改保存产品类型与操作工位
     */
    @RequiresPermissions("central:centralProductTypeOperationStation:edit")
    @Log(title = "产品类型与操作工位", businessType = BusinessType.UPDATE)
    @PutMapping
    @ResponseBody
    public AjaxResult editSave(@Validated(UpdateGroup.class) @RequestBody TopCentralProductTypeOperationStation topCentralProductTypeOperationStation)
    {
        return toAjax(topCentralProductTypeOperationStationService.updateById(topCentralProductTypeOperationStation));
    }

    /**
     * 修改产品类型与操作工位 状态 根据 ids 主键
     */
    @RequiresPermissions("central:centralProductTypeOperationStation:edit")
    @Log(title = "产品类型与操作工位", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStateByIds")
    @ResponseBody
    public AjaxResult updateStateByIds(@Validated(value= UpdateBatchGroup.class)@RequestBody StateVo<Integer> stateVo)
    {
        return toAjax(topCentralProductTypeOperationStationService.updateStateByIds(stateVo.getIds(),stateVo.getState()));
    }

    /**
     * 删除产品类型与操作工位
     */
    @RequiresPermissions("central:centralProductTypeOperationStation:remove")
    @Log(title = "产品类型与操作工位", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ResponseBody
    public AjaxResult deleteByIds(@PathVariable("ids") List<Integer> ids)
    {
            return toAjax(topCentralProductTypeOperationStationService.deleteByIds(ids));
    }
}
